/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import Dto.CatalogoDTO;
import Modelo.Conexion;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author -ADMIN-
 */
public class CatalogoDAO {
    
    public static Connection con = Conexion.getInstance();
    public static PreparedStatement pstm;
    public static CallableStatement cstm;
    public static ResultSet rs;
    
    
     public static synchronized int nuevoCatalogo(String nombre, String descripcion) {
        String call = "{CALL  sp_registrarCatalogo(?,?,?)}";
        int rpta = 0;
        try {
            con.setAutoCommit(false);
            cstm = con.prepareCall(call);
            cstm.setString(1, nombre);
            cstm.setString(2, descripcion);
            cstm.registerOutParameter(3, java.sql.Types.INTEGER);
            cstm.execute();
            rpta = cstm.getInt(3);
            /* si la salida es 0 ya existe ese catalogo, sino si salida = 1 proceso exitoso*/
            if(rpta == 1){
                con.commit();
            }else{
                Conexion.deshacerCambios(con);
            }
        }catch (SQLException e){
            throw new RuntimeException("error al registrar el nuevo catalogo : "+e.getMessage());
        }
        
         return rpta;
     }
     
    public static synchronized boolean eliminarCatalogo(String id ){
          Boolean rpta = false;
          String delete = "delete from Catalogos where NombreCatalogo = ?";
          try{
              pstm= con.prepareStatement(delete);
              pstm.setString(1, id);
              
              
              if(pstm.executeUpdate() > 0)
                  rpta = true;
              
              
          }catch(SQLException sqe){
              throw new RuntimeException("error: "+sqe.getSQLState()+" = "+sqe.getMessage());
          }
          return rpta;
      }
    
    public static synchronized boolean updateCatalogos(String idCatalogo, String descripcion){
        String sql = "{sp_updateCatalogo(?, ?, ?)}";
        boolean salida = false;
        try{
           cstm = con.prepareCall(sql);
           cstm.setString(1, idCatalogo);
           cstm.setString(2, descripcion);
           cstm.registerOutParameter(3, java.sql.Types.INTEGER);
           cstm.execute();
           
           int result = cstm.getInt(3);
           if(result == 1)
               salida = true;
           
           }catch(SQLException sq){
               throw new RuntimeException("erro: "+sq.getSQLState()+" = "+sq.getMessage());
           }
        
        return salida;
    }
    
     public static synchronized List<CatalogoDTO> consultarCatalogos(){
         ArrayList<CatalogoDTO> lista = new ArrayList<>();
         String listadoCatalogo = "select NombreCatalogo, descripcionCatalogo from Catalogos where 1 = 1";
         try{
            pstm = con.prepareStatement(listadoCatalogo);
            rs = pstm.executeQuery();
            while(rs.next()){
                lista.add(new CatalogoDTO(rs.getString("NombreCatalogo"), rs.getString("descripcionCatalogo")));
            }
            
         }catch(SQLException se){
             throw new RuntimeException("error: "+se.getSQLState()+" = "+se.getMessage());
         }
            return lista;
     }
     
     
     
}
